var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var parentid = 0;
var sids = 0;
var gid = 0;

$(document).ready(function(){
	start();
	showline();
	$("#unasign").hide();
	$("#asign").show();
	$("#btn_add").click(function(){
		alert(parentid);
		if(parentid==0){
			alert("请选择父结点");
		}else{
			$("#unasign").show();
			$("#asign").hide();
			$("#assiges").show();
			$("#unassiges").hide();
		}
	});
	$("#btn_edit").click(function(){
		if(gid==0){
			alert("请选择要修改的结点");
		}else{
			$.ajax({
		        type: "get",
		        url: postPath + '/group/selectGroup',
		        data : {id:sids},  
		        success : function(data) {
		    		$("#dbKey").val(data.groupVO.groupName);
		    		$("#dbName").val(data.groupVO.menuPrompt);
		    		$("#dbUrl").val(data.groupVO.style);
		    		$("#beizhu").val(data.groupVO.status);
		    		$("#server").val(data.groupVO.serverName);
		    		$("#test").val(data.groupVO.status);
		        },  
		        error : function() {  
		            alert('error...');  
		        }  
		     });
			$("#unasign").show();
			$("#asign").hide();
			$("#assiges").hide();
			$("#unassiges").show();
			
		}
	});
	$("#btn_adit").click(function(){
		edited();
	});
	function edited(){
		var name = $("#dbKey").val();
		var tishi = $("#dbName").val();
		var style = $("#dbUrl").val();
		var beizhu = $("#beizhu").val();
		var server = $("#server").val();
		var test = $("#test option:selected").val();
		alert(name);
		alert(tishi);
		alert(style);
		if(name = ""){
			alert("请输入要修改的组名");
		}else if(tishi = ""){
			alert("请输入要修改的组的提示");
		}else if(style = ""){
			alert("请输入要修改的组的类型");
		}else if(beizhu = ""){
			alert("请输入要修改的组的备注");
		}else if(server = ""){
			alert("请输入要修改的组的服务名");
		}
		 var jsons = {  
		            pid:sids,  
		        name:$("#dbKey").val(),
		        tishi:$("#dbName").val(),
		        style:$("#dbUrl").val(),
		        server:$("#server").val(),
		        beizhu:$("#beizhu").val(),
		        test:$("#test option:selected").val()
		        };
		 //var postdata = JSON.stringify(json);
		 
		 $.ajax({
		        type: "get",
		        url: postPath + '/group/updateGroup',
		        data : jsons,  
		        success : function(data) {
		        	alert(data.message);
		        	if("修改成功".eqlues(data.message)){
		        		 alert("修改成功");  
		        	}
		        },  
		        error : function() {  
		            alert('修改--成功');
		            start();
		        }  
		     });
	}
	$("#btn_del").click(function(){
		if(sids==0){
			alert("请选择要删除的结点");
		}else{
			$.ajax({
		        type: "get",
		        url: postPath + '/group/deleteGroup',
		        data : {id:sids},  
		        success : function(data) {
		        	alert("11111111111111");
		    		alert("删除成功");
		        },  
		        error : function() {
		        	alert("shanhcu");
		            alert('删除成功');  
		        }  
		     });
		}
	});
	$("#btn_save").click(function(){
		var name = $("#dbKey").val();
		var tishi = $("#dbName").val();
		var style = $("#dbUrl").val();
		var beizhu = $("#beizhu").val();
		var server = $("#server").val();
		var test = $("#test option:selected").val();
		if(name = ""){
			alert("请输入要添加的组名");
		}else if(tishi = ""){
			alert("请输入要添加的组的提示");
		}else if(style = ""){
			alert("请输入要添加的组的类型");
		}else if(beizhu = ""){
			alert("请输入要添加的组的备注");
		}else if(server = ""){
			alert("请输入要添加的组的服务名");
		}
		 var jsons = {  
		            pid:parentid,  
		        name:$("#dbKey").val(),
		        tishi:$("#dbName").val(),
		        style:$("#dbUrl").val(),
		        server:$("#server").val(),
		        beizhu:$("#beizhu").val(),
		        test:$("#test option:selected").val()
		        };
		 //var postdata = JSON.stringify(json);
		 
		 $.ajax({
		        type: "get",
		        url: postPath + '/group/insertGroup',
		        data : jsons,  
		        success : function(data) {
		        	alert(data.message);
		        	if("添加成功".eqlues(data.message)){
		        		 alert("添加成功");  
		        	}
		        },  
		        error : function() {  
		            alert('添加成功');
		            start();
		        }  
		     });
	});
	
	
	function onClick(event, treeId, treeNode) {
		$("#unasign").hide();
		$("#asign").show();
		var sid = treeNode.id;
		$.ajax({
	        type: "get",
	        url: postPath + '/group/selectGroup',
	        data : {id:sid},  
	        success : function(data) {
	    		$("#dbKeys").val(data.groupVO.groupName);
	    		$("#dbNames").val(data.groupVO.menuPrompt);
	    		$("#dbUrls").val(data.groupVO.style);
	    		$("#beizhu").val(data.groupVO.status);
	    		$("#server").val(data.groupVO.serverName);
	    		$("#test").val(data.groupVO.status);
	        },  
	        error : function() {  
	            alert('error...');  
	        }  
	     });
	}
	function onCheck(e, treeId, treeNode){
		parentid = 0;
		parentid = treeNode.pid;
		sids =  treeNode.id;
		gid = treeNode.id;
		var sid = treeNode.id;
		onClick(e, treeId, treeNode);
	}
	function showline(){
		$.ajax({
	        type: "get",
	        url: postPath + '/group/selectOne',
	        data : {},  
	        success : function(data) {
	    		$("#dbKeys").val(data.groupVO.groupName);
	    		$("#dbNames").val(data.groupVO.menuPrompt);
	    		$("#dbUrls").val(data.groupVO.style);
	    		$("#beizhu").val(data.groupVO.status);
	    		$("#server").val(data.groupVO.serverName);
	    		$("#test").val(data.groupVO.status);
	        },  
	        error : function() {  
	            alert('error...');  
	        }  
	     });
	}
	function start(){
		alert("dahsabi1");
		//左侧tree数据绑定	 
		var setting = {	
				check: {
					enable: true
				},
			data: {
				key:{
					title:"hint"
				},
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: onClick,
				onCheck: onCheck
				}
		};
		var zNodes=[{"id":2,"name":"河北","children":[{"id":3,"name":"石家庄","pid":2}],"pid":1},{"id":4,"name":"河南","children":[{"id":5,"name":"郑州","pid":4},{"id":6,"name":"濮阳","pid":4}],"pid":1}];
		$.ajax({
	        type : 'get',
	        url : postPath + '/group/selectAllGroupGson',
	        async : false,
	        success : function(res) {
	        	alert("+_+_+_+_+"+res.str);
//	        	zNodes=res.str;
//	        	alert("dazhiweixin");
//	        	alert(zNodes);
	        },
	        error : function() {  
	            alert('error...');  
	        }  
	    });
		$.fn.zTree.init($("#tree"), setting, zNodes);
	}
});
